using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantSubmarinePickUpAnimationController
/// \brief  Launch the invincibility animation
[AddComponentMenu("GK/AbyssalGiant/Submarine/gkAbyssalGiantSubmarineInvincibilityPhaseAnimationController")]
public class gkAbyssalGiantSubmarineInvincibilityPhaseAnimationController : MonoBehaviour 
{
	/// The pickup animation state
	private AnimationState m_rInvinciblityAnimationState;
	
	/// \brief  Called just after the game begin to play
	void Start()
	{
		// Grab the invincibility animation state
		m_rInvinciblityAnimationState = animation["Submarine_Invincibility"];
		
		// Place the invincibilty phase on the effects animation layer
		m_rInvinciblityAnimationState.layer = 1;
	}
	
	/// \brief  Called when the component is enabled
	void OnEnable() 
	{
		// Listen the invincibility event
		GetComponent<gkEventComponent>().AddEventHandler(gkAbyssalGiantInvincibiliyEvent.InvincibilityBegin, HandleInvincibilityBeginEvent);
		GetComponent<gkEventComponent>().AddEventHandler(gkAbyssalGiantInvincibiliyEvent.InvincibilityEnd, HandleInvincibilityEndEvent);
	}
	
	/// \brief  Called when the component is disabled
	void OnDisable() 
	{	
		// Remove the invincibility event
		GetComponent<gkEventComponent>().RemoveEventHandler(gkAbyssalGiantInvincibiliyEvent.InvincibilityBegin, HandleInvincibilityBeginEvent);
		GetComponent<gkEventComponent>().RemoveEventHandler(gkAbyssalGiantInvincibiliyEvent.InvincibilityEnd, HandleInvincibilityEndEvent);
	}
	
	/// Handle the invincibility begin event
	void HandleInvincibilityBeginEvent(Object a_rEvent)
	{
		animation.Blend(m_rInvinciblityAnimationState.name, 1.0f, 0.0f);
		m_rInvinciblityAnimationState.time = 0.0f;
	}
	
	/// Handle the invincibility begin event
	void HandleInvincibilityEndEvent(Object a_rEvent)
	{
		animation.Blend(m_rInvinciblityAnimationState.name, 0.0f, 0.0f);
	}
}